package com.open.basic.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;


/**
 * @author: system
 * @Date: 2024/9/24 上午11:39
 * @Version: V1.0
 * @description: ->
 */

public class SocketBIO implements SocketIO {


    private InputStream inputStream;
    private boolean connected;
    private OutputStream outputStream;

    public void connect(String host, int port) {
        try {
            Socket socket = new Socket(host, port);
            socket.setSoTimeout(10000);
            SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
            socket.connect(remoteSocketAddress, 10000);
            boolean connected = socket.isConnected();
            if (connected) {
                socket.setSoTimeout(10000);
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean init() throws IOException {
        return false;
    }

    @Override
    public void write(final byte b) throws IOException {
        outputStream.write(b);
        outputStream.flush();
    }

    @Override
    public void write(final byte[] bytes) throws IOException {
        outputStream.write(bytes);
        outputStream.flush();
    }

    @Override
    public byte read() throws IOException {
        return (byte) inputStream.read();
    }

    @Override
    public int read(final byte[] b, final int off, final int len) throws IOException {
        return inputStream.read(b, off, len);
    }

    @Override
    public void flush() throws IOException {
        outputStream.flush();
    }

    @Override
    public boolean isConnected() {
        return this.connected;
    }

    @Override
    public boolean isAlive() {
        return false;
    }

    @Override
    public byte[] readLineBytes() throws IOException {
        return new byte[0];
    }

    @Override
    public void release() {

    }

    @Override
    public String getHost() {
        return "";
    }

    @Override
    public int getPort() {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }

    @Override
    public int getVersion() {
        return 0;
    }

    @Override
    public boolean isMaster() {
        return false;
    }

    @Override
    public boolean auth(final String password) {
        return false;
    }

    @Override
    public boolean isAuthed() {
        return false;
    }

    @Override
    public void setAuthed(final boolean authed) {

    }
}
